/*
 * @Author: your name
 * @Date: 2021-07-06 16:27:57
 * @LastEditTime: 2021-07-27 13:51:22
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \cuiyuan_activity\assets\script\Game.ts
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


const { ccclass, property } = cc._decorator;

@ccclass
export default class Game extends cc.Component {



    static inst: Game;
    gameStatus: number = 0;// 主游戏状态 暂时未用到
    mainBgm: cc.AudioSource;//主背景音乐

    onLoad() {
        let canvas = cc.find("Canvas");


        Game.inst = this;
        this.mainBgm = this.node.getComponent(cc.AudioSource)
        this.init();

    }

    // 初始化场景
    init() {
        this.nextScene("Begin")
    }



    // 主背景音乐控制
    playMainBgm() {
        this.mainBgm.play()
        this.setMainBgmVolume();
        this.mainBgm.loop = true;
    }

    // 主背景音乐暂停
    pauseMainBgm() {
        if (this.mainBgm) {
            this.mainBgm.pause();
        }
    }

    /**
     * @description 设置主背景音乐的音量 做一个缓动的处理 让音效不那么突兀
     * @param value 音量
     */
    setMainBgmVolume(value: number = 1) {
        cc.tween(this.mainBgm)
            .to(2, { volume: value })
            .start()
    }

    /**
     * @description 获取节点下的ts脚本
     * @param path 脚本路径 必传
     * @param name (可选参数) 脚本名称 如果不传，默认是传入的path
     * @returns 
     */
    getScript(path: string, name?: string) {
        return cc.find(`Canvas/${path}`).getComponent(name || path)
    }

    /**
     * @description 展示下一个场景
     * @param name 场景名称 每个场景对应相应的脚本，通过脚本进行展示
     */
    nextScene(name: string) {
        let nextScene = this.getScript(name)
        nextScene.show();
    }
    /**
     * @description 隐藏当前场景
     * @param name 场景名称 每个场景对应相应的脚本，通过脚本进行展示
     */

    hideScene(name: string) {
        let currentScene = this.getScript(name)
        currentScene.hide();
    }





}
